package com.gqz.aircraftwar.mvp.model.aircraft

import android.graphics.*
import com.gqz.aircraftwar.App
import com.gqz.aircraftwar.mvp.model.buff.BuffManager
import com.gqz.aircraftwar.mvp.model.bullet.BulletManager
import com.gqz.aircraftwar.util.MyEnum
import com.gqz.aircraftwar.util.d
import com.gqz.aircraftwar.util.i
import com.gqz.aircraftwar.util.w
import kotlinx.coroutines.*
import java.lang.Exception
import kotlin.system.measureNanoTime
import kotlin.system.measureTimeMillis

/**
 * 玩家飞机
 *
 * @author gqz
 * @data 2020/3/23
 */

class PlayerAircraft private constructor() : Aircraft() {

    private val TAG: String = "PlayerAircraft-->"

    val interval = 1000 / 30L

    var job1: Job? = null
    var job2: Job? = null
    var job3: Job? = null

    var ctx = newSingleThreadContext("Player")

    private object Holder {
        val instance = PlayerAircraft()
    }

    companion object {
        fun getInstant() = Holder.instance
    }

    init {
        hp = 10f
        armor = 50
        speed = 8f
        attackSpeed = 0.25f//1.2f
        atk_interval = (1000f / attackSpeed).toLong()
        dir = MyEnum.Dir.up
        free = false
    }

    fun reset() {
        hp = 1f
        armor = 5
        speed = 8f
        attackSpeed = 3.5f
        free = false
    }

    fun start() {
        try {
            job1 = GlobalScope.launch(ctx) {
                i(TAG, "start--> player worked in ${Thread.currentThread().name}")
                while (App.isRunning && !free) {
//                    d(TAG, "running-->")
                    if (hp <= 0) {
                        free = true
                    }
                    if (movable) {
                        move(move_x, move_y)
                    }
                    delay(interval)
                }
            }
            job2 = GlobalScope.launch(ctx) {
                i(TAG, "start--> player worked in ${Thread.currentThread().name}")
                while (App.isRunning && !free) {
//                    d(TAG, "running-->")
//                    BulletManager.instance.generatePlayerBullet(
//                        PointF(centerX(), pos.y), 0f, 15f, 2f, 1
//                    )
                    if (containBuff(MyEnum.BuffType.Scattering)) {
                        BulletManager.instance.generateScatteringBullet(
                            PointF(centerX(), pos.y), 10f, 1f
                        )
                    } else {
//                        BulletManager.instance.generatePlayerBullet(
//                            PointF(centerX(), pos.y),
//                            0f,
//                            15f
//                        )
                        BulletManager.instance.generateCannonBullet(PointF(centerX(), pos.y), 3f)
                    }
                    delay(atk_interval)
                }
            }
            job3 = GlobalScope.launch(ctx) {
                i(TAG, "start--> player worked in ${Thread.currentThread().name}")
                while (App.isRunning) {
                    checkBuff()
                    delay(1000)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun stop() {
        job1?.cancel()
        job2?.cancel()
        job3?.cancel()
    }

}
